#include "login_page.h"

#include <QCloseEvent>
#include <QDebug>
#include <QHBoxLayout>
#include <QIcon>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QFile>
#include <QTextEdit>
#include <QTextStream>
#include <qboxlayout.h>

#include "ElaWindow.h"
#include "ElaApplication.h"
#include "ElaPushButton.h"
#include "ElaImageCard.h"
#include "ElaText.h"

#include "register.h"
LoginPage::LoginPage(QWidget* parent)
    : ElaWidget(parent)
{
    setWindowTitle("登陆");
    setWindowModality(Qt::ApplicationModal);
    setIsStayTop(true);
    setWindowFlags(windowFlags() | Qt::Tool);   //不在windows的任务栏显示，防止用任务栏关闭登录界面。
    setWindowButtonFlags(ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint);//登录界面隐藏关闭按钮。
    // setWindowIcon(QIcon(":/include/Image/Moon.jpg"));
    // this->setIsFixedSize(true);
    // setWindowModality(Qt::ApplicationModal);
    // setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
    // ElaImageCard* pixCard = new ElaImageCard(this);
    // pixCard->setFixedSize(60, 60);
    // pixCard->setIsPreserveAspectCrop(false);
    // pixCard->setCardImage(QImage(":/include/Image/Moon.jpg"));
    //
    // QVBoxLayout* pixCardLayout = new QVBoxLayout();
    // pixCardLayout->addWidget(pixCard);
    // pixCardLayout->addStretch();
    //
    // ElaText* versionText = new ElaText("ElaWidgetTools-LK-2024", this);
    // QFont versionTextFont = versionText->font();
    // versionTextFont.setWeight(QFont::Bold);
    // versionText->setFont(versionTextFont);
    // versionText->setWordWrap(false);
    // versionText->setTextPixelSize(18);
    //
    // ElaText* licenseText = new ElaText("MIT授权协议", this);
    // licenseText->setWordWrap(false);
    // licenseText->setTextPixelSize(14);
    // ElaText* supportText = new ElaText("Windows支持版本: QT5.12以上\nLinux支持版本: Qt5.14以上", this);
    // supportText->setWordWrap(false);
    // supportText->setTextPixelSize(14);
    // ElaText* contactText = new ElaText("作者: 8009963@qq.com\n交流群: 850243692(QQ)", this);
    // contactText->setWordWrap(false);
    // contactText->setTextInteractionFlags(Qt::TextSelectableByMouse);
    // contactText->setTextPixelSize(14);
    // ElaText* helperText = new ElaText("用户手册及API文档付费提供\n提供额外的专业技术支持", this);
    // helperText->setWordWrap(false);
    // helperText->setTextPixelSize(14);
    // ElaText* copyrightText = new ElaText("版权所有 © 2024 Liniyous", this);
    // copyrightText->setWordWrap(false);
    // copyrightText->setTextPixelSize(14);
    //
    // QVBoxLayout* textLayout = new QVBoxLayout();
    // textLayout->setSpacing(15);
    // textLayout->addWidget(versionText);
    // textLayout->addWidget(licenseText);
    // textLayout->addWidget(supportText);
    // textLayout->addWidget(contactText);
    // textLayout->addWidget(helperText);
    // textLayout->addWidget(copyrightText);
    // textLayout->addStretch();
    //
    // QHBoxLayout* contentLayout = new QHBoxLayout();
    // contentLayout->addSpacing(30);
    // contentLayout->addLayout(pixCardLayout);
    // contentLayout->addSpacing(30);
    // contentLayout->addLayout(textLayout);
    //
    // QVBoxLayout* mainLayout = new QVBoxLayout(this);
    // mainLayout->setContentsMargins(0, 25, 0, 0);
    // mainLayout->addLayout(contentLayout);

    LEdit0 = new QLineEdit(this);
    LEdit1 = new QLineEdit(this);

    QLabel *text0 = new QLabel(this);
    QLabel *text1 = new QLabel(this);
    text0->setText("账号");
    text1->setText("密码");
    text0->move(110,70);
    text1->move(110,125);

    LEdit0->setParent(this);
    LEdit1->setParent(this);
    LEdit0->move(300,70);
    LEdit1->move(300,125);
    LEdit1->setEchoMode(QLineEdit::Password);

    ElaPushButton *btn0 = new ElaPushButton("登录",this);
    ElaPushButton *btn1 = new ElaPushButton("注册",this);
    ElaPushButton *closeBtn = new ElaPushButton("退出",this);
    // btn0->move(110,180);
    // btn1->move(220,180);

    QVBoxLayout* mainLayout = new QVBoxLayout();
    QHBoxLayout* userLayout = new QHBoxLayout();
    QHBoxLayout* passLayout = new QHBoxLayout();

    userLayout->addWidget(text0);
    userLayout->addWidget(LEdit0);

    passLayout->addWidget(text1);
    passLayout->addWidget(LEdit1);

    mainLayout->addLayout(userLayout);
    mainLayout->addLayout(passLayout);
    mainLayout->addWidget(btn0);
    mainLayout->addWidget(btn1);
    mainLayout->addWidget(closeBtn);

    setLayout(mainLayout);

    Register *r = new Register();
    connect(btn1, &ElaPushButton::clicked, r, &QWidget::show);
    connect(btn0, &ElaPushButton::clicked, this, &LoginPage::SignIN);
    connect(closeBtn,&ElaPushButton::clicked,this,[=]()
    {
        windowClosed();
        this->close();
    });

    // ElaApplication::connect(this, &ElaWindow::userInfoCardClicked, this, [&](){
    //     if(flag == 0) {
    //         this->show();
    //     } else {
    //         this->Xiaoxi();
    //     }
    // });

}

LoginPage::~LoginPage()
{
}

void LoginPage::SignIN()
{
    QString str0,str1;

    str0 = LEdit0->text();
    str1 = LEdit1->text();
    Paras(str0,str1);
}
void LoginPage::Paras(QString Account,QString Password)
{
    int m = 0,n = 0,p = 0;
    QString strA,strP,strN;
    int flagAcc = 0;
    if(Account == "") {
        QMessageBox::critical(this,"","请输入账号");
    } else if(Password == "") {
        QMessageBox::critical(this,"","请输入密码");
    } else {
        QString fileName = filePath + "/Account.txt";
        QFile f(fileName);
        f.open(QIODevice::ReadOnly);
        QTextStream in(&f);
        while(!in.atEnd()) {
            strA = in.readLine();
            m++;
            if(strA.toUtf8() == Account.toUtf8()) {
                flagAcc = 1;
                Acc = strA;
                f.close();
            }
            if(flagAcc == 1) {
                QFile ff;
                fileName = filePath + "/Password.txt";
                qDebug()<<fileName;
                qDebug()<<m;
                qDebug()<<n;
                ff.setFileName(fileName);
                QTextStream in1(&ff);
                ff.open(QIODevice::ReadOnly);
                {
                    while(!in1.atEnd()) {
                        strP = in1.readLine();
                        n++;
                        if(m==n) {
                            if(strP == Password) {
                                flag = 1;
                                QMessageBox::information(this,"","登录成功");
                                LEdit0 ->clear();
                                LEdit1 ->clear();
                                ff.close();
                                this ->close();
                                break;
                            } else {
                                QMessageBox::critical(this,"","账号或密码不正确");
                                ff.close();
                                break;
                            }
                        }
                    }
                }
                break;
            }
        }
        if(flag == 1) {
            //f.setFileName("D:\\name.txt");
            //f.open(QIODevice::ReadOnly);
            QFile fff("D:\\name.txt");
            QTextStream in2(&fff);
            fff.open(QIODevice::ReadOnly);
            while(!in2.atEnd()) {
                strN = in2.readLine();
                p++;
                if(p == m) {
                    Name = strN;
                    fff.close();
                    break;
                }
            }
            flagChangedSignal();
        }
        if(flagAcc == 0) {
            QMessageBox::critical(this,"","该账号不存在");
            flagAcc = 0;
            f.close();
        }
    }
}

void LoginPage::Xiaoxi()
{
    int ret = QMessageBox::question(this ,"注销","您想要注销吗？", QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    if (ret == QMessageBox::Yes) {
        // 用户选择了 "Yes"，执行删除操作
        // ...
        this->flag = 0;
        flagChangedSignal();
    } else if (ret == QMessageBox::No) {
        // 用户选择了 "No"，不执行删除操作
        // ...
        this ->flag = 1;
    } else {
         this ->flag = 1;
    }
}

// // TODO: move to qthread or drop
// void threadFunction(ElaWindow *w, MainWindow *w0, QPixmap icon)
// {
//     while (1) {
//         if(w0->flag == 0) {
//             w->setUserInfoCardTitle("请登录");
//             w->setUserInfoCardSubTitle("登录以使用更多功能");
//             w->setUserInfoCardPixmap(icon);
//             w0->video_stop();
//         } else {
//             w->setUserInfoCardTitle(w0->Name);
//             w->setUserInfoCardSubTitle(w0->Acc);
//             w->setUserInfoCardPixmap(icon);
//         }
//     }
// }
//
